package com.zzptc.zzsgz.bdsjws.activities;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.fragments.Search_Contacts_Fragment;
import com.zzptc.zzsgz.bdsjws.fragments.SecondFragment;
import com.zzptc.zzsgz.bdsjws.fragments.ShowAllcontacts;
import com.zzptc.zzsgz.bdsjws.vo.Contacts;

import java.util.ArrayList;
import java.util.List;

public class Add_Contacts_Activity extends BaseActivity implements View.OnClickListener {

    private ShowAllcontacts frameLayout;
    private EditText editText;
    private Search_Contacts_Fragment search_fragment;
    private static TextView textView;
    private boolean isReplace;
    private boolean isClose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int getContentXML() {
        return R.layout.activity_add__contacts_;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        findViewById(R.id.img_back_addcontacts).setOnClickListener(this);
        findViewById(R.id.btn_true).setOnClickListener(this);
        textView = (TextView) findViewById(R.id.contacts_count_num);
        editText = (EditText) findViewById(R.id.edit_search_contacts);
        editText.addTextChangedListener(textWatcher);
        if (frameLayout==null){
            frameLayout=new ShowAllcontacts();
        }
        getFragmentManager().beginTransaction().replace(R.id.frameLayout_add_contacts,frameLayout).commit();
    }

    TextWatcher textWatcher=new TextWatcher() {

        public List<Contacts> list=new ArrayList<>();

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(final CharSequence charSequence, int i, int i1, int i2) {
            if(charSequence.length()>0&&!isReplace){
                if (search_fragment==null){
                    search_fragment=new Search_Contacts_Fragment();
                }
                isReplace=true;
                isClose = false;
                getFragmentManager().beginTransaction().replace(R.id.frameLayout_add_contacts, search_fragment).addToBackStack("contact").commit();
            }else if (charSequence.length()==0&&isReplace){
                if (frameLayout==null){
                    frameLayout=new ShowAllcontacts();
                }
                getFragmentManager().beginTransaction().replace(R.id.frameLayout_add_contacts,frameLayout).addToBackStack("contact").commit();
                isReplace=false;
                isClose = true;
            }
            new Thread(){
                @Override
                public void run() {
                    list.clear();
                    list .addAll( ShowAllcontacts.search(charSequence.toString().replace(" ","").replace("-","")) );
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(search_fragment!=null) {
                                search_fragment.setList(list,list.size());
                            }
                        }
                    });
                }
            }.start();
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.img_back_addcontacts:
                finish();
                break;
            case R.id.btn_true:
                finish();
                SecondFragment.onNotifyDataSetChanged();
                break;
        }
    }

    public static void getTextViewContactsCount(String str){
    }

    @Override
    public void onBackPressed() {
        if(getFragmentManager().getBackStackEntryCount() == 2){
            getFragmentManager().popBackStack();
            editText.setText("");
        }else {
            if (isClose) {
                finish();
                SecondFragment.onNotifyDataSetChanged();
            } else {
                int backStackCount = getFragmentManager().getBackStackEntryCount();
                for (int i = 0; i < backStackCount; i++) {
                    getFragmentManager().popBackStack();
                }
                isClose = true;
                editText.setText("");
            }

        }
    }

}
